﻿using System;
using System.Collections.Generic;
using MediaBrowser.Library.Entities;
using MBWebService.Core.Classes;
using System.IO;

namespace MBWebService.Core
{
    public static class Extensions
    {
        public static bool TryParseEnum<T>(this string value, out T output) where T : struct
        {
            bool retVal = false;
            output = default(T);
            try
            {
                output = (T)Enum.Parse(typeof(T), value, true);
                retVal = true;
            }
            catch (ArgumentNullException) { }
            catch (ArgumentException) { }

            return retVal;
        }

        public static T ToEnum<T>(this string value) where T : struct
        {
            T retVal;
            value.TryParseEnum(out retVal);
            return retVal;
        }

        public static IEnumerable<BasicItem> ToBasicEnumerable(this IList<BaseItem> items)
        {
            List<BasicItem> retVal = new List<BasicItem>();
            foreach (var item in items)
                retVal.Add(new BasicItem(item));
            return retVal;
        }

        public static void EnsureDirectoryExistance(this DirectoryInfo dir)
        {
            if (!Directory.Exists(dir.FullName))
            {
                Directory.CreateDirectory(dir.FullName);
            }
        }

        public static bool IsNullOrEmpty(this string stringVal)
        {
            return string.IsNullOrEmpty(stringVal);
        }

        public static string ToStringHHmmss(this TimeSpan timeSpan)
        {
            DateTime tempdate = DateTime.MinValue.Add(timeSpan);
            string retVal = tempdate.ToString("HH:mm:ss");

            return retVal;
        }
    }
}
